package ca.badenhorst.ourtrition.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by wade on 05/03/14.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "nutrition.db";
    public static final int DATABASE_VERSION = 1;

    private static final String TYPE_PRIMARY_KEY = "INTEGER PRIMARY KEY AUTOINCREMENT";

    public static final String GROUPS_TABLE = "food_groups";
    private static final String[] GROUP_COLUMNS = new String[]{"id", "groupCd", "description"};
    public static final String[] GROUP_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "TEXT"};

    public static final String FOODS_TABLE = "foods";
    public static final String[] FOOD_COLUMNS = new String[]{"id", "ndb_no", "name", "food_group_cd", "facet"};
    public static final String[] FOOD_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "TEXT", "TEXT", "TEXT"};

    public static final String NUTRITION_TABLE = "nutritions";
    public static final String[] NUTRITION_COLUMNS = new String[]{"id", "nutr_no", "units", "tag", "description"};
    public static final String[] NUTRITION_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "TEXT", "TEXT", "TEXT"};

    public static final String SIZE_TABLE = "sizes";
    public static final String[] SIZE_COLUMNS = new String[]{"id", "ndb_no", "seq", "amount", "description", "weight_gms"};
    public static final String[] SIZE_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "INTEGER", "REAL", "TEXT", "REAL"};

    public static final String FOOD_NUTRITION_TABLE = "food_nutrition";
    public static final String[] FOOD_NUTRITION_COLUMNS = new String[]{"id", "ndb_no", "nutr_no", "amount"};
    public static final String[] FOOD_NUTRITION_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "TEXT", "REAL"};


    public static final String TABLE_FOOD_EATEN = "foods_eaten";
    public static final String[] FOOD_EATEN_COLUMNS =  new String[]{"id", "ndb_no", "amount","entered_timestamp"};
    public static final String[] FOOD_EATEN_TYPES = new String[]{TYPE_PRIMARY_KEY, "TEXT", "REAL", "DATETIME DEFAULT CURRENT_TIMESTAMP"};

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION); // the constructor just sets the values and returns, therefore context cannot be null
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(createTable(FOODS_TABLE, FOOD_COLUMNS, FOOD_TYPES));
        database.execSQL(createTable(GROUPS_TABLE, GROUP_COLUMNS, GROUP_TYPES));
        database.execSQL(createTable(NUTRITION_TABLE, NUTRITION_COLUMNS, NUTRITION_TYPES));
        database.execSQL(createTable(SIZE_TABLE, SIZE_COLUMNS, SIZE_TYPES));
        database.execSQL(createTable(FOOD_NUTRITION_TABLE, FOOD_NUTRITION_COLUMNS, FOOD_NUTRITION_TYPES));

        database.execSQL(createTable(TABLE_FOOD_EATEN, FOOD_EATEN_COLUMNS, FOOD_EATEN_TYPES));
    }

    private String createTable(String tableName, String[] columns, String[] types) {
        if (tableName == null || columns == null || types == null || types.length != columns.length || types.length == 0) {
            throw new IllegalArgumentException("Invalid parameters for creating table " + tableName);
        } else {
            StringBuilder stringBuilder = new StringBuilder("CREATE TABLE ");

            stringBuilder.append(tableName);
            stringBuilder.append(" (");
            for (int n = 0, i = columns.length; n < i; n++) {
                if (n > 0) {
                    stringBuilder.append(", ");
                }
                stringBuilder.append(columns[n]).append(' ').append(types[n]);
            }
            return stringBuilder.append(");").toString();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        if (newVersion==2)
        {

        }
    }

}

